package general.guicontrols.clusterpanels;

import general.art.Node;
import general.art.WeightedCenteredCluster;
import general.exceptions.NodeNotFoundException;
import general.guicontrols.layoutmanager.CircularLayout;
import java.sql.SQLException;
import java.util.Map;
import java.util.HashMap;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JPanel;

import viewdatabase.CircularGUI;

public class CircularDisplay extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private WeightedCenteredCluster cluster;

	private final Map<Node, NodePanel> nodeToNodePanel;
	private final Map<Node, MouseOverDisplay> nodeToCircularDisplay;
	private final CircularLayout cl;
	public CircularDisplay(WeightedCenteredCluster c){

		
		nodeToNodePanel = new HashMap<Node, NodePanel>();
		nodeToCircularDisplay = new HashMap<Node, MouseOverDisplay>();
		cl = new CircularLayout(c, nodeToNodePanel, nodeToCircularDisplay);
		cluster = c;
		createContentFromCLuster(c);
		
		
	}

	private void createContentFromCLuster(WeightedCenteredCluster c){

		//Map<Component, Node>  cToN = new HashMap<Component, Node>();

		//nodeToNodePanel = new HashMap<Node, NodePanel>();

		for(Node n : c.getNodes()){

			if (n != c.getSeedNode()){
				NodePanel jb = CreateNodePanel(n);
				//cToN.put(jb, n);
				nodeToNodePanel.put(n, jb);
				this.add(jb);

			} /*else {

				NodePanel seedNode = new CreateNodePanel(n);
				cToN.put(seedNode, n);
				this.add( seedNode);


			}*/

		}
		
		this.setLayout( cl );


	}


	/**
	 * 
	 * 
	 */
	private NodePanel CreateNodePanel(final Node n){


		MouseAdapter d = new MouseAdapter(){

			@Override
			public void mouseClicked(MouseEvent arg0) {

				CircularDisplay.this.removeAll();
				nodeToCircularDisplay.clear();
				nodeToNodePanel.clear();
				
				try {
					cluster = new WeightedCenteredCluster(n.getKey(), cluster.getDepth());
					createContentFromCLuster(cluster);
				} catch (InstantiationException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				} catch (SQLException e) {
					e.printStackTrace();
				} catch (NodeNotFoundException e) {
					e.printStackTrace();
				}


				//CircularGUI.createInterFace(n.getKey(), mDepth);
				//thisParent.creatNewPanel(n.getKey());
				CircularDisplay.this.revalidate();
				CircularDisplay.this.repaint();
			}

			@Override
			public void mouseEntered(MouseEvent arg0){

				//System.out.println("Entered Field");

				try {
					final MouseOverDisplay innerDisplay = new MouseOverDisplay( new WeightedCenteredCluster(n.getKey(), cluster.getDepth()));
					
					if( nodeToCircularDisplay.isEmpty()){
					
						setNodeGray();
						
						
					nodeToCircularDisplay.put(n, innerDisplay);
					innerDisplay.setPreferredSize( new Dimension(600, 600));
					innerDisplay.addMouseListener(  new  MouseAdapter(){

						@Override
						public void mouseExited(MouseEvent e) {
							
							setNodeBlack();
							
							nodeToCircularDisplay.remove(n);
							CircularDisplay.this.remove(innerDisplay);
							CircularDisplay.this.revalidate();
							CircularDisplay.this.repaint();
						}

					});


					CircularDisplay.this.add( innerDisplay);

					}

				} catch (InstantiationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NodeNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				CircularDisplay.this.revalidate();
				CircularDisplay.this.repaint();

			}

			public void mouseExited(MouseEvent arg0){


			}

		};

		NodePanel tempB  = new NodePanel(n, d);
		tempB.setPreferredSize( new Dimension(100, 100));
		//tempB.setName(n.getName());

		return tempB;


	}

	private void setNodeGray(){
		
		for( NodePanel greyedOutNodes : nodeToNodePanel.values()){
			
			greyedOutNodes.setForeground(Color.GRAY);
		}
		
	}
	
	private void setNodeBlack(){
		
		for( NodePanel greyedOutNodes : nodeToNodePanel.values()){
			
			greyedOutNodes.setForeground(Color.BLACK);
		}
		
		
	}
	

}
